#!/bin/bash
# Hell Flappy Bird Android APK 构建脚本

echo "🎮 Hell Flappy Bird - Android APK 构建脚本"
echo "=========================================="

# 检查Python版本
echo "📋 检查Python环境..."
python --version
python -c "import sys; print(f'Python版本: {sys.version}')"

# 检查Kivy安装
echo "📋 检查Kivy安装..."
python -c "import kivy; print(f'Kivy版本: {kivy.__version__}')" || {
    echo "❌ Kivy未安装，正在安装..."
    pip install kivy kivymd
}

# 检查buildozer安装
echo "📋 检查Buildozer安装..."
python -c "import buildozer; print('Buildozer已安装')" || {
    echo "❌ Buildozer未安装，正在安装..."
    pip install buildozer cython
}

# 检查系统依赖
echo "📋 检查系统依赖..."
if ! command -v java &> /dev/null; then
    echo "❌ Java未安装，请安装OpenJDK 8"
    exit 1
fi

# 初始化buildozer配置
echo "📋 初始化Buildozer配置..."
if [ ! -f "buildozer.spec" ]; then
    echo "初始化buildozer.spec..."
    buildozer init
fi

# 检查buildozer.spec配置
echo "📋 检查构建配置..."
if grep -q "requirements = python3,kivy" buildozer.spec; then
    echo "更新requirements配置..."
    sed -i 's/requirements = python3,kivy/requirements = python3,kivy,kivymd,pillow/' buildozer.spec
fi

# 开始构建
echo "🚀 开始构建Android APK..."
echo "这可能需要几分钟时间，请耐心等待..."

# 构建APK
buildozer -v android debug

# 检查构建结果
if [ -f "bin/*.apk" ]; then
    echo "✅ APK构建成功！"
    echo "📱 APK文件位置:"
    ls -la bin/*.apk
    echo ""
    echo "📋 安装说明:"
    echo "1. 将APK文件传输到Android设备"
    echo "2. 在设备上启用'未知来源'安装"
    echo "3. 点击APK文件进行安装"
    echo "4. 享受游戏！"
else
    echo "❌ APK构建失败！"
    echo "请检查构建日志中的错误信息"
    exit 1
fi

echo ""
echo "🎉 构建完成！"
